package com.xxl.job.core.biz.client;

import com.xxl.job.core.biz.ExecutorBiz;
import com.xxl.job.core.biz.model.*;
import com.xxl.job.core.util.XxlJobRemotingUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * admin api test
 *
 * @author xuxueli 2017-07-28 22:14:52
 */
public class ExecutorBizClient implements ExecutorBiz {

    private static final Logger logger = LoggerFactory.getLogger(ExecutorBizClient.class);

    public ExecutorBizClient() {
    }
    public ExecutorBizClient(String addressUrl, String  accessCookie) {
        this.addressUrl = addressUrl;
        this.accessCookie = accessCookie;

        // valid
        if (!this.addressUrl.endsWith("/")) {
            this.addressUrl = this.addressUrl + "/";
        }
    }

    private String addressUrl ;
    private String accessCookie;
    private int timeout = 3;


    @Override
    public ReturnT<String> beat(String cookie) {
        return XxlJobRemotingUtil.postBody(addressUrl+"beat", cookie, timeout, "", String.class);
    }

    @Override
    public ReturnT<String> idleBeat(IdleBeatParam idleBeatParam, String cookie){
        return XxlJobRemotingUtil.postBody(addressUrl+"idleBeat", cookie, timeout, idleBeatParam, String.class);
    }

    @Override
    public ReturnT<String> run(TriggerParam triggerParam, String cookie) {
        return XxlJobRemotingUtil.postBody(addressUrl + "run", cookie, timeout, triggerParam, String.class);
    }

    @Override
    public ReturnT<String> kill(KillParam killParam, String cookie) {
        return XxlJobRemotingUtil.postBody(addressUrl + "kill", cookie, timeout, killParam, String.class);
    }

    @Override
    public ReturnT<LogResult> log(LogParam logParam, String cookie) {
        return XxlJobRemotingUtil.postBody(addressUrl + "log", cookie, timeout, logParam, LogResult.class);
    }

    @Override
    public ReturnT<String> runPool(TriggerParam triggerParam, String cookie) {
        return XxlJobRemotingUtil.postBody(addressUrl + "runpool", cookie, timeout, triggerParam, String.class);
    }

    @Override
    public ReturnT<String> runBatch(List<TriggerParam> triggerParamList, String cookie) {
        return XxlJobRemotingUtil.postBody(addressUrl + "runbatch", cookie, timeout, triggerParamList, String.class);
    }

}
